Source code for src.cmd.util

import os, shutil, MySQLdb, re

from ..segment import all_segments, Segment
from ..configuration import config
__all__ = ['MySQL', 'segment_run_map', 'create_log_dir', 'get_threads', 'validate_project', 'check_pass']

segment_run_map = {'a':'arrest', 
                   'c':'sentence', 
                   'd':'demographic', 
                   's':'supervision',
                   'su':'subject'}

_hasUpper = re.compile('[A-Z]')
_haslower = re.compile('[a-z]')
_hasNum = re.compile('[0-9]')

[docs]class MySQL(object): def __init__(self, user, pwd): self.conn = MySQLdb.connect(host=config.db_host, user=self.user, passwd=self.pwd, port=config.db_port) self.cur = conn.cursor() def __enter__(self): return self def __exit__(self, type, value, traceback): self.conn.close() self.cur.close()
[docs]def check_pass(pwd): return True if len(pwd) > 6 and _hasUpper.search(pwd) and _hasLower.search(pwd) else False
[docs]def create_log_dir(log_dir, replace=False): if not os.path.isdir(log_dir): os.makedirs(log_dir) elif replace: shutil.rmtree(log_dir) os.makedirs(log_dir) return log_dir
[docs]def validate_project(project, verbose): p_dir = os.path.join(config.root_dir, 'project_data', project) if not os.path.isdir(p_dir): if verbose: print 'Invalid project %s' % project return False return True
[docs]def get_threads(cases, cycles): threads = config.transform_threads if config.transform_threads >= 250 else 250 """Basic function to determine number of threads to use.""" _set_min = lambda t: t if t >= 10 else 10 x = cases / 25000 #assume a baseline sample size of 75000 if x > 3: #adjust if sample size is larger than baseline threads = int(threads - ((x - 3)/4)*threads) elif x < 3: #for now we do not adjust for smaller sample threads = threads if _set_min(threads) == 10: return threads #if we set to < 0 return 10 threads x = cycles / 100 #assume a baseline of 100 cycles if x > 1: #adjust if cycles is larger than baseline threads = int(threads - ((x - 1)/2)*threads) elif x < 1: #for now we do not adjust for smaller cycles threads = threads print threads return _set_min(threads)